discard

use "$healthsave\raw_prevalence_w7.dta", clear
set more off 

/****************************/
/*** MEDICATION VARIABLES ***/
/****************************/

tokenize LungDisease Asthma Arthritis Osteoporosis Cancer Parkinsons ///
				 PsychiatricProblems Alzheimers Dementia BloodDisorder 		 ///
				 Hypertension Angina HeartAttack HeartFailure HeartMurmur	 ///
				 HeartRhythm Diabetes Stroke Cholesterol	
		
		foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
			forv i = 0/7 { 
				
				gen med`x'_w`i' = . 
				lab var med`x'_w`i' "Reports taking medication/treatment for `1'" 
				
				gen nmed`x'_w`i' = . 
				lab var nmed`x'_w`i' "Total reports of medication for `1' up to and including wave `i'" 	
			}
			mac shift 
		}


		
	/* medication variables in HSE 
		(only for conditions where a doctor's diagnosis is asked 
		 about specifically) */ 	
		
	/* stroke */	
		 replace medst_w0 = 1 if ///
						 medheart == 1 		/// /* reports medication for stroke/heart condition */
					  &  strodef  == 1   	/// /* reports having been diagnosed with a stroke */
					  &  everstro == 1 		/// /* and having had a stroke (routing) */
					  &  everhart == 2      /// /* and not having had any of the heart conditions that would also have routed them into the question */
					  &  everireg == 2 		///
					  &  everoht  == 2 		///
					  &  everangi == 2
	/* heart murmur */	
		 replace medhm_w0 = 1 if /// 
						murpillw0 == 1		///
						| admurw0 == 1 		///
						| mursurgw0 == 1 
	
	/* diabetes */ 
		 replace meddi_w0 = 1 if /// 
						medcindi == 1 | ///
						insulin  == 1 | ///
						advicedi == 1 
	
	/* hypertension */
		replace medbp_w0 = 1 if bpmedd==1 | pastabbpw0==1 
		
		
	/* medication variables common to all ELSA waves */ 	
		
		forv i = 1/7 {
		
		tokenize helng heama hecanb hemda hemdb 
			foreach x in lu as ca bp di {
				replace med`x'_w`i' = (`1'w`i'==1) if `1'w`i'>=-1 & `1'w`i'!=. 
				if "`x'"=="di" & heinsw`i'==1 replace med`x'_w`i' = 1
				mac shift 
			}	
		}

		
	/* only in later waves */	
		

		forv i = 3/7 {
		tokenize hecanbb hechmd   helngb heamb hemdab hechme 
			
			foreach x in bl ch {
				replace med`x'_w`i' = (`1'w`i'==1) if `1'w`i'==1 | `1'w`i'==2
			mac shift 
			}

			foreach x in lu as bp ch {
				replace med`x'_w`i'=1 if `1'w`i'==1
			mac shift 
			}
		}

		
	/* reports treatment for depression - only waves 2 and 4 */
		** W2 question: depression: action recommended by doctor 
		** W4 question: depression: taken medication in last 2 yrs/counselling

		replace medps_w2 = (hepsyaw2==1) if hepsyaw2>0 & hepsyaw2!=.
		replace medps_w4 = (hepsyaw4==1) if hepsyaw4>0 & hepsyaw2!=.

	/* osteoporosis */	
		
		forv i = 5/7 { 
			replace medos_w`i' = (heostecw`i'==1) if heostecw`i'>0 & heostecw`i'!=.
		} 
		
		

	/** heart attack **/
	
		/* only record medication if there is reason to believe 
			that medication is for that condition. 
			eg if report of a condition routed that person into that 
			question or if they only report one condition. */ 
			
		replace medmi_w2 = (hehrtbw2==1)*(everan_w2!=1) if inwave2==1 
		replace medmi_w2 = 1 if hebetbw2==1 | hebetbw2==3
		lab var medmi_w2 "Taking anticoagulant medication (and no angina) or beta-blocker" 

		replace medmi_w3 = (hehrtmdw3==1)*(everan_w3!=1)  if inwave3==1 
		lab var medmi_w3 "Taking anticoagulant medication (and no angina)" 
		replace medmi_w3 = 0 if (everan_w3==0 | everan_w3==-1) & evermi_w3==0 		
		
		replace medmi_w4 = (hehrtmdw4==1)*(everan_w4!=1 & everst_w4!=1 & everdi_w4!=1) if inwave4==1 
		lab var medmi_w4 "Taking anticoagulant medication (and no angina, stroke or diabetes - other conditions routed in)" 
		replace medmi_w4 = 0 if everan_w4==0 & evermi_w4==0 		
		
		
		forv i = 5/7 { 
			replace medmi_w`i' = (hehrtbw`i'==1 | hehrtcw`i'==1)*(everan_w`i'==0) if inwave`i'==1 
			lab var medmi_w`i' "Taking anticoagulant medication or warfarin (no angina)"
			replace medmi_w`i' = 0 if evermi_w`i' == 0 & everan_w`i' == 0 
		} 
		
		
	/** angina **/		
		
		/* only record medication if there is reason to believe 
			that medication is for that condition. 
			eg if report of a condition routed that person into that 
			question or if they only report one condition. */ 
			
			
		replace medan_w2 = (hehrtbw2==1)*(evermi_w2!=1) if inwave2==1
		lab var medan_w2 "Taking anticoagulant medication (and no hatta) or beta-blocker" 

		replace medan_w3 = (hehrtmdw3==1)*(evermi_w3!=1) if inwave3==1 
		lab var medan_w3 "Taking blood-thinning medication (and no hatta)" 
		replace medan_w3 = 0 if everan_w3==0 & evermi_w3==0 		

		replace medan_w4 = (hehrtmdw4==1)*(evermi_w4!=1 & everst_w4!=1 & everdi_w4!=1) if inwave4==1 
		lab var medan_w4 "Taking blood-thinning medication (and no hatta, stroke or diabetes - other conditions routed in)" 
		replace medan_w4 = 0 if everan_w4==0 & evermi_w4==0 		

		forv i = 5/7 { 
			replace medan_w`i' = (hehrtbw`i'==1 | hehrtcw`i'==1)*(evermi_w`i'==0) if inwave`i'==1 
			lab var medan_w`i' "Taking anticoagulant medication or warfarin (no hatta)"
			replace medan_w`i' = 0 if evermi_w`i' == 0 & everan_w`i' == 0 
		
		}
		
		
	
		/* how many people essentially report both angina and heart attacks, 
			making it hard to know what the medication is 'for' */
		
		cd "P:\ELSA\Healthdynamics\documentation\tables\" 

		pause on 
		preserve 
		gen meds_w1 = . 
		gen meds_w2 = (hehrtbw2==1) 
		gen meds_w3 = (hehrtmdw3==1) 
		gen meds_w4 = (hehrtmdw4==1) 
		gen meds_w5 = (hehrtbw5==1 | hehrtcw5==1) 
		gen meds_w6 = (hehrtbw6==1 | hehrtcw6==1) 
		gen meds_w7 = (hehrtbw7==1 | hehrtcw7==1) 
		
		keep idauniq meds_w* medan_w* medmi_w* evermi_w? everan_w?
		reshape long meds_w  medan_w  medmi_w  evermi_w  everan_w, i(idauniq) j(wave) 
		
		gen ambig_meds = meds_w == 1 & medmi==0 & medan==0 
		
		tabout wave if meds_w == 1 using how_many_report_medication_an_mi.txt, ///
			c(sum medan_w sum medmi_w sum ambig_meds count meds_w) f(0) sum replace 
		restore 
	




*******************************************
**        MEASURED HYPERTENSION          **
*******************************************

***    WAVE 0 

	* Available for those who did not refuse nurse visit and who had valid bp readings
	* hypertensive if meausured blood pressure higher than 140/90 OR if taking medication specifically for hbp
	* non-hypertensive those who are not hypertensive and who are receiving medication not specifically for hbp but that lowers hbp 
	gen bp_rec_w0=(hyper140==2 | hyper140==3 | hyper140==4)
	replace bp_rec_w0=. if hyper140<0 | hyper140==. 
	label var bp_rec_w0 "nurse measured hypertension or taking medication for hypertension in wave 0"

***    WAVE 2 
	gen bp_rec_w2 = (sysvalw2>140) if sysvalw2!=-1 & sysvalw2!=. 
	replace bp_rec_w2=1 if diavalw2>90 & diavalw2!=-1 & diavalw2!=. 
	replace bp_rec_w2 = 1 if hemdaw4==1
	label var bp_rec_w2 "nurse measured hypertension or taking medication for hypertension in wave 2" 


***    WAVE 4
	gen bp_rec_w4 = (sysvalw4>140) if sysvalw4!=-1 & sysvalw4!=. 
	replace bp_rec_w4=1 if diavalw4>90 & diavalw4!=-1 & diavalw4!=. 
	replace bp_rec_w4=1 if hemdabw4==1 | hemdaw4==1 

	label var bp_rec_w4   "nurse measured hypertension or taking medication for hypertension in wave 4" 

***    WAVE 6
	gen bp_rec_w6 = (sysvalw6>140) if sysvalw6!=-1 & sysvalw6!=. 
	replace bp_rec_w6=1 if diavalw6>90 & diavalw6!=-1 & diavalw6!=. 
	replace bp_rec_w6=1 if hemdabw6==1 | hemdaw6==1 

	label var bp_rec_w6   "nurse measured hypertension or taking medication for hypertension in wave 6" 

	
	drop hehrt* hepsyaw2 hepsyaw4 hepsybw4 heostecw5 heostecw6
	


/** tot no. waves reported medication inc current wave   */
 
	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
		replace nmed`x'_w0 = (med`x'_w0==1) 
		forvalues i=1/7 {
			local Pi=`i'-1
			replace nmed`x'_w`i' = nmed`x'_w`Pi' + (med`x'_w`i'==1) 
		}
	}

	
/** tot no. waves hypertension measured inc current wave */
	
	qui: gen nbp_rec_w0 	= (bp_rec_w0==1)
	qui: gen nbp_rec_w1 	= nbp_rec_w0
	qui: gen nbp_rec_w2 	= nbp_rec_w1 + (bp_rec_w2==1) 
	qui: gen nbp_rec_w3 	= nbp_rec_w2
	qui: gen nbp_rec_w4  	= nbp_rec_w3 + (bp_rec_w4==1) 
	qui: gen nbp_rec_w5     = nbp_rec_w4
	qui: gen nbp_rec_w6     = nbp_rec_w5 + (bp_rec_w4==1) 
	qui: gen nbp_rec_w7     = nbp_rec_w6 
	

	forvalues i=0/7 {
		label var nbp_rec_w`i' "total occasions measured as hypertensive up to and including wave `i'" 
	}
	
	save "$healthsave\with medication_w7.dta", replace


	preserve 
	keep idauniq meddi* medbp* 
	reshape long meddi_w medbp_w , i(idauniq) j(wave) 	
	drop if wave==0 
	save "$healthsave\medication_info_w7.dta", replace 
	restore 



